package calendar_ex;

import java.util.*;

//
// Represents tags using a Trie.
// This was abstracted so that you cannot directly add tags that are unrelated
// to appointments from the CalenderEx class. Or something.
//
class Tags {
    private HashMap<String, Integer> tags;
    
    //
    // Constructor
    //
    public Tags() {
        this.tags = new HashMap<String, Integer>();
    }
    
    //
    // Adds one occurance of a tag to the Trie.
    //
    public void add(String s) {       
        if (this.tags.containsKey(s) == false)
            this.tags.put(s, 1);
        else
            this.tags.put(s, this.tags.get(s) + 1);
    }
    
    //
    // Removes one occurance of a tag from the Trie.
    //
    public void remove(String s) {
        if (this.tags.containsKey(s) == false)
            return;
        
        if (this.tags.get(s) == 1)
            this.tags.remove(s);
        else
            this.tags.put(s, this.tags.get(s) - 1);
    }
    
    //
    // Wrapper for the Trie's getWordsStartingWith() method.
    //
    public List<String> getTagsStartingWith(String s) {
        LinkedList<String> t = new LinkedList<String>();
        
        for (String tag: this.tags.keySet())
            if (tag.startsWith(s) == true)
                t.add(tag);
                
        return Collections.unmodifiableList(t);
    }
    
    //
    // Returns a list of all tags.
    // 
    public List<String> getTags() {
        LinkedList<String> t = new LinkedList<String>();
        
        for (String tag: this.tags.keySet())
            t.add(tag);
                
        return Collections.unmodifiableList(t);
    }
}